<!-- 车辆配置信息 -->
<template>
    <div >
        <a-page-header
            style="border: 1px solid rgb(235, 237, 240);background:#fff"
            :title="`${vehicleName}-参数信息`"
            @back="() => $router.go(-1)"
        >
            <div class='table-page-search-wrapper'>
                <a-form layout='inline'>
                    <a-row :gutter='48'>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="参数名称">
                                <a-select v-model="paramTitleId" placeholder="请选择" allowClear @change="queryCont">
                                    <a-select-option v-for="(item,i) in paramTitleList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="参数内容">
                                <a-select v-model="paramContentId" placeholder="请选择" allowClear @change="getData">
                                    <a-select-option v-for="(item,i) in paramContentList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item >
                                <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                                <a-button type="" icon="plus" @click="add()">添加</a-button>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </div> 

            <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small">
                <div slot="imageUrl" slot-scope="text,record">
                    <img style="width: 120px;height: 60px" v-if="record.imageUrl && record.imageUrl.substring(0,4) == 'http'" :src="record.imageUrl && record.imageUrl.split(',')[0]" alt="" @click="showImg(record.imageUrl)">
                    <div v-else>-</div>
                </div>
                <div slot="paramTitleId" slot-scope="paramTitleId">
                    <div v-for="(item,i) in paramTitleList" :key="item.id">
                        <a-tag v-if="item.id == paramTitleId" :color="colors[i%13]">{{item.name}}</a-tag>
                    </div>
                </div>
                <div slot="paramContentName" slot-scope="paramContentName">
                    <div v-if="paramContentName">{{paramContentName}}</div>
                    <div v-else>-</div>
                </div>
                <div slot="status" slot-scope="status">
                    <a-switch checked-children="开" un-checked-children="关" disabled :checked="status == '1' ? true : false" />
                </div>
                <div slot="operation" slot-scope="text,record">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前配置?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button class="errorColor" type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </a-table>
        </a-page-header> 

        <a-modal
            title="添加、修改车辆参数信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="参数标题">
                        <a-select v-model="item.paramTitleId" placeholder="请选择车辆参数标题" allowClear @change="getContentForm">
                            <a-select-option v-for="(item,i) in paramTitleList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="参数标题">
                        <a-select v-model="item.paramContentId" placeholder="请选择车辆参数标题" allowClear>
                            <a-select-option v-for="(item,i) in paramContentFormList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 上传图片预览 -->
        <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false">
            <img alt="example" style="width: 100%" :src="previewImage" />
        </a-modal>
        <!-- 表格图片预览 -->
        <a-modal :visible="previewImgVisible" :footer="null" @cancel="previewImgVisible = false">
            <a-carousel autoplay dots-class="slick-dots slick-thumb" arrows style="margin-bottom:60px">
                <div v-for="(img,i) in showImgs" :key="i">
                    <img :src="img" alt=""/>
                </div>
                <a slot="customPaging" slot-scope="props">
                    <img :src="getImgUrl(props.i)" />
                </a>
            </a-carousel>
        </a-modal>
    </div>
</template>

<script>
import { queryTravelVehicleParamConfig , updateTravelVehicleParamConfig , queryTravelVehicleParamTitle , queryTravelVehicleParamContent } from '@/api/travel';
import { columns } from './table';
import { uploadFile } from '@/api/brand';
import { getBase64 } from '@/utils/util';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            vehicleName: this.$route.query.vehicleName,
            vehicleId: this.$route.query.vehicleId,
            paramTitleId: undefined,
            paramTitleList: [],
            paramContentId: undefined,
            paramContentList: [],
            paramContentFormList: [],
            status: undefined,
            statusList: [
                { id: '1', name: '开启'},{ id: '0', name: '关闭'}
            ],
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            columns,
            item: {
                'id': '',
                'paramTitleId': undefined, // 参数标题
                'paramContentId': undefined, // 参数内容
            },
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: false,
            visible: false,
            imgList: [],
            previewVisible: false,
            previewImage: '',
            previewImgVisible: false,
            showImgs: [],
        };
    },
    async created(){
        try {
            await this.queryTitle();
            this.getData();
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 获取车辆参数标题列表
        queryTitle(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryTravelVehicleParamTitle({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.paramTitleList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 获取车辆参数标题内容
        queryContent(titleId,form){
            let _this = this
            return new Promise((resolve,reject) => {
                queryTravelVehicleParamContent({'size': 99,'page': 0,'titleId': titleId})
                .then(res=>{
                    if(res.success){
                        if(form == '1'){
                            _this.paramContentFormList = res.data.content;
                        }else{
                            _this.paramContentList = res.data.content;
                        }
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询车辆配置信息
        getData(){
            let _this = this;
            _this.isload = true
            const params = {
                'brandId': _this.brandId,
                'paramTitleId': _this.paramTitleId,
                'paramContentId': _this.paramContentId,
                'vehicleId': _this.vehicleId,
                'page': _this.page - 1,
                'size': _this.size,
            }
            queryTravelVehicleParamConfig(params)
            .then(res => {
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content;
                    _this.total = res.data.totalElements;
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData();
        },
        // 添加产品
        add(){
            this.item.id = ''
            this.item.vehicleId = this.vehicleId;
            this.item.paramTitleId = undefined;
            this.item.paramContentId = undefined;
            this.visible = true
        },
        // 修改产品
        async update(item){
            let _this = this;
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认添加、修改车型分类信息
        async handleOk(){ 
            let _this = this
            if(_this.item.paramTitleId == undefined){
                _this.$message.warning('请选择车辆参数标题')
                return;
            }
            if(_this.item.paramContentId == undefined){
                _this.$message.warning('请选择车辆参数内容')
                return;
            }
            const params = {
                'id':_this.item.id,
                'brandId': _this.brandId,
                'vehicleId': _this.item.vehicleId,
                'paramTitleId': _this.item.paramTitleId,
                'paramContentId': _this.item.paramContentId,
            }
            updateTravelVehicleParamConfig(params)
            .then(async res=>{
                if(res.success){
                    _this.visible = false
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除产品
        del(item){
            let _this = this
            const params = {
                'id':item.id,
                'vehicleId': item.vehicleId,
                'paramTitleId': item.paramTitleId,
                'paramContentId': item.paramContentId,
                'isDelete':'1',
            }
            updateTravelVehicleParamConfig(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 预览图片
        showImg(imgs){
            if(imgs){
                this.showImgs = imgs.split(',')
                console.log(this.showImgs,'imgs')
                this.previewImgVisible = true
            }
        },
        getImgUrl(i){
            return this.showImgs[i]
        },
        async handlePreview(file) {
            if (!file.url && !file.preview) {
                file.preview = await getBase64(file.originFileObj);
            }
            this.previewImage = file.url || file.preview;
            this.previewVisible = true;
        },
        imgHandleChange({ fileList }) {
            this.imgList = fileList;
        },
        beforeUpload(file) {
            return false;
        },
        // 上传图片获取图片url
        uploadImg(file,name){
            let _this = this
            return new Promise((resolve,reject) => {
                let formData = new FormData()
                formData.append('brandId',_this.brandId)
                formData.append('urlName',name)
                formData.append('file',file)
                uploadFile(formData)
                .then(res=>{
                    if(res.success){
                        resolve(res.data)
                    }else{
                        reject(res.errMessage);
                    }
                })
            })
        },
        // 查询参数标题
        async queryCont(id){
            console.log(id,'id')
            if(id){
                await this.queryContent(id);
            }else{
                this.paramContentList = [];
            }
            this.getData();
        },
        async getContentForm(id){
            if(id){
                await this.queryContent(id,'1');
            }else{
                this.paramContentFormList = [];
            }
        }
    }
}

</script>
<style scoped>

</style>